package com.example.phone.account.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.phone.R;
import com.example.phone.base.RequestInterface;
import com.example.phone.bean.Order;
import com.example.phone.bean.OrderItem;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019\1\2 0002.
 */

class ProductRecycleAdapter extends RecyclerView.Adapter<ProductRecycleAdapter.ViewHolder> {
    private Order order;
    private Context context;
    public ProductRecycleAdapter(Context context, Order order) {
        this.order = order;
        this.context = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.product_img)
        ImageView productImg;
        @BindView(R.id.product_name)
        TextView productName;
        @BindView(R.id.product_price)
        TextView productPrice;
        @BindView(R.id.product_account)
        TextView productAccount;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this,view);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        OrderItem orderItem = order.getOrderItems().get(position);
        //设置图片
        String imgPath = RequestInterface.baseImgPath + orderItem.getProductImage();
        Log.d("orderAdapter", imgPath);
        Glide.with(context)
                .load(imgPath)
                .into(holder.productImg);
        holder.productName.setText(orderItem.getProductName());
        holder.productPrice.setText(orderItem.getCurrentUnitPrice()+"");
        holder.productAccount.setText("×"+orderItem.getQuantity() + "");

    }

    @Override
    public int getItemCount() {
        return order.getOrderItems().size();
    }
}
